/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.ddtroops.serviceclasses;

import cz.muni.fi.pa165.ddtroops.daointerfaces.SkillDAO;
import cz.muni.fi.pa165.ddtroops.dto.DTOFactory;
import cz.muni.fi.pa165.ddtroops.dto.SkillDTO;
import cz.muni.fi.pa165.ddtroops.entities.EntityBuilder;
import cz.muni.fi.pa165.ddtroops.entities.Skill;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.SkillService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Erik
 */
@Service
@Qualifier("skillService")
public class SkillServiceImpl implements SkillService {
    @Autowired(required=true)
    SkillDAO dao;

    public void setDao(SkillDAO dao) {
        this.dao = dao;
    }
    
    @Transactional
    @Secured("ROLE_SUPERVISOR")
    public SkillDTO createNewSkill(String name, String description, String profession, Long minXP)
    {
        Skill s = new EntityBuilder<Skill>(Skill.class)
                .withProperty("Name", name)
                .withProperty("Description", description)
                .withProperty("Profession", profession)
                .withProperty("minXP", minXP)
                .Build();

        return DTOFactory.createSkillDTO(s);
    }

    @Transactional
    @Secured("ROLE_SUPERVISOR")
    public void create(SkillDTO skillDTO)
    {
        dao.create(DTOFactory.createSkill(skillDTO));
    }

    @Transactional
    @Secured("ROLE_SUPERVISOR")
    public void update(SkillDTO skillDTO)
    {
        dao.update(DTOFactory.createSkill(skillDTO));
    }

    @Transactional
    @Secured("ROLE_SUPERVISOR")
    public void delete(SkillDTO skillDTO)
    {
        dao.delete(DTOFactory.createSkill(skillDTO));
    }

    @Transactional(readOnly=true)
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public SkillDTO getById(Long id)
    {
        Skill s = dao.getById(id);
        return (s!=null)?DTOFactory.createSkillDTO(s):null;
    }
    @Transactional(readOnly=true)
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public List<SkillDTO> getAll()
    {
        List<SkillDTO> skillDTOs = new ArrayList<SkillDTO>();
        for (Skill s : dao.getAll()) {
            skillDTOs.add(DTOFactory.createSkillDTO(s));
        }
        return skillDTOs;
    }
    
    @Transactional(readOnly=true)
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public List<SkillDTO> getByProfession(String profession) {
        List<SkillDTO> skillDTOs = new ArrayList<SkillDTO>();
        for (Skill s : dao.getByProfession(profession)) {
            skillDTOs.add(DTOFactory.createSkillDTO(s));
        }
        return skillDTOs;
    }

    @Transactional(readOnly=true)
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public List<SkillDTO> getByXp(Long xp) {
        List<SkillDTO> skillDTOs = new ArrayList<SkillDTO>();
        for (Skill s : dao.getByXp(xp)) {
            skillDTOs.add(DTOFactory.createSkillDTO(s));
        }
        return skillDTOs;
    }

    
    public List<SkillDTO> getByProfessionAndXp(String profession, Long xp) {
        List<SkillDTO> skillDTOs = new ArrayList<SkillDTO>();
        for (Skill s : dao.getByProfessionAndXp(profession, xp)) {
            skillDTOs.add(DTOFactory.createSkillDTO(s));
        }
        return skillDTOs;
    }
}
